﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Manage
{
    public partial class MainFrame : Form
    {
        public MainFrame()
        {
            InitializeComponent();
            loadClasses();
            loadStudent();
        }

        private void loadClasses()
        {
            AptechManager am = AptechManager.Read();
            if (am.AptechClasses != null)
            {
                gridClass.Rows.Clear();
                comboBoxStudentClassID.Items.Clear();
                foreach (AptechClass clazz in am.AptechClasses)
                {
                    gridClass.Rows.Add(clazz.ID, clazz.Name, clazz.TimeSlot, clazz.Room, clazz.Teacher);
                    comboBoxStudentClassID.Items.Add(clazz);
                }
                if (am.AptechClasses.Count > 0)
                    comboBoxStudentClassID.SelectedIndex = 0;
            }
        }

        private void drawTableClasses(List<AptechClass> clazzs)
        {
            if (clazzs != null)
            {
                gridClass.Rows.Clear();
                foreach (AptechClass clazz in clazzs)
                {
                    gridClass.Rows.Add(clazz.ID, clazz.Name, clazz.TimeSlot, clazz.Room, clazz.Teacher);
                }
            }
        }

        private void drawTableStudent(List<AptechStudent> students)
        {
            if (students != null)
            {
                gridStudent.Rows.Clear();
                foreach (AptechStudent student in students)
                {
                    gridStudent.Rows.Add(student.ID, student.Name, student.ClassID, student.Address);
                }
            }
        }

        private void loadStudent()
        {
            AptechManager am = AptechManager.Read();
            drawTableStudent(am.AptechStudents);
        }

        private void MainFrame_Load(object sender, EventArgs e)
        {

        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnAddClass_Click(object sender, EventArgs e)
        {
            AptechManager am = AptechManager.Read();
            AptechClass clazz = new AptechClass();
            clazz.Name = textBoxName.Text;
            clazz.TimeSlot = textBoxTimeSlot.Text;
            clazz.Room = textBoxRoom.Text;
            clazz.Teacher = textBoxTeacher.Text;
            // Set toan bo tu giao dien vao
            am.addClass(clazz);
            AptechManager.Save(am);
            textBoxName.Text = "";
            textBoxTimeSlot.Text = "";
            textBoxRoom.Text = "";
            textBoxTeacher.Text = "";
            loadClasses();
        }   

        private void btnAddStudent_Click(object sender, EventArgs e)
        {
            AptechManager am = AptechManager.Read();
            AptechStudent student = new AptechStudent();
            student.ClassID = ((AptechClass)comboBoxStudentClassID.SelectedItem).ID;
            student.Name = textBoxStudentName.Text;
            student.Address = textBoxStudentAddress.Text;
            // Set toan bo tu giao dien vao
            am.addStudent(student);
            AptechManager.Save(am);
            textBoxStudentName.Text = "";
            textBoxStudentAddress.Text = "";
            loadStudent();
        }

        private void btnEditClass_Click(object sender, EventArgs e)
        {
            AptechManager am = AptechManager.Read();
            AptechClass clazz = new AptechClass();
            DataGridViewRow row = gridClass.SelectedRows[0];
            string id = row.Cells["ClassID"].Value.ToString();
            MessageBox.Show("ID: " + id);
            clazz.ID = int.Parse(id);
            clazz.Name = textBoxName.Text;
            clazz.TimeSlot = textBoxTimeSlot.Text;
            clazz.Room = textBoxRoom.Text;
            clazz.Teacher = textBoxTeacher.Text;
            am.updateClass(clazz);
            AptechManager.Save(am);
            textBoxName.Text = "";
            textBoxTimeSlot.Text = "";
            textBoxRoom.Text = "";
            textBoxTeacher.Text = "";
            loadClasses();
            
        }

        private void btnEditStudent_Click(object sender, EventArgs e)
        {
            AptechManager am = AptechManager.Read();
            AptechStudent student = new AptechStudent();
            DataGridViewRow row = gridStudent.SelectedRows[0];
            string id = row.Cells["StudentID"].Value.ToString();
            MessageBox.Show("ID: " + id);
            student.ClassID = ((AptechClass)comboBoxStudentClassID.SelectedItem).ID;
            student.Name = textBoxStudentName.Text;
            student.Address = textBoxStudentAddress.Text;
            am.updateStudent(student);
            AptechManager.Save(am);
            textBoxStudentName.Text = "";
            textBoxStudentAddress.Text = "";
            loadStudent();
        }

        private void btnDeleteClass_Click(object sender, EventArgs e)
        {
            if (gridClass.SelectedRows.Count == 0) return;
            DataGridViewRow row = gridClass.SelectedRows[0];
            string id = row.Cells["ClassID"].Value.ToString();
            AptechManager am = AptechManager.Read();
            am.deleteClass(int.Parse(id));
            AptechManager.Save(am);
            loadClasses();
        }

        private void btnDeleteStudent_Click(object sender, EventArgs e)
        {
            if (gridStudent.SelectedRows.Count == 0) return;
            DataGridViewRow row = gridStudent.SelectedRows[0];
            string id = row.Cells["StudentID"].Value.ToString();
            AptechManager am = AptechManager.Read();
            am.deleteStudent(int.Parse(id));
            AptechManager.Save(am);
            loadStudent();
        }

        private void btnSearchClass_Click(object sender, EventArgs e)
        {
            string input = "";
            ShowInputDialog(ref input);
            AptechManager am = AptechManager.Read();
            List<AptechClass> clazzs = am.searchClass(input);
            drawTableClasses(clazzs);
        }


        private void btnSearchStudent_Click(object sender, EventArgs e)
        {
            string input = "";
            ShowInputDialog(ref input);
            AptechManager am = AptechManager.Read();
            List<AptechStudent> students = am.searchStudent(input);
            drawTableStudent(students);
        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

       

        private void gridClass_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
        
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void comboBoxStudentClassID_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void lblTeacherClass_Click(object sender, EventArgs e)
        {

        }

        private void textBoxName_TextChanged(object sender, EventArgs e)
        {

        }

        private void lblNameClass_Click(object sender, EventArgs e)
        {

        }

        private void labelName_Click(object sender, EventArgs e)
        {

        }

        private void labelAddress_Click(object sender, EventArgs e)
        {

        }

        private void labelClassID_Click(object sender, EventArgs e)
        {

        }

        private void tabPage2_Click(object sender, EventArgs e)
        {

        }


        private static DialogResult ShowInputDialog(ref string input)
        {
            System.Drawing.Size size = new System.Drawing.Size(200, 70);
            Form inputBox = new Form();

            inputBox.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            inputBox.ClientSize = size;
            inputBox.Text = "Search";

            System.Windows.Forms.TextBox textBox = new TextBox();
            textBox.Size = new System.Drawing.Size(size.Width - 10, 23);
            textBox.Location = new System.Drawing.Point(5, 5);
            textBox.Text = input;
            inputBox.Controls.Add(textBox);

            Button okButton = new Button();
            okButton.DialogResult = System.Windows.Forms.DialogResult.OK;
            okButton.Name = "okButton";
            okButton.Size = new System.Drawing.Size(75, 23);
            okButton.Text = "&Search";
            okButton.Location = new System.Drawing.Point(size.Width - 80 - 80, 39);
            inputBox.Controls.Add(okButton);

            Button cancelButton = new Button();
            cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            cancelButton.Name = "cancelButton";
            cancelButton.Size = new System.Drawing.Size(75, 23);
            cancelButton.Text = "&Cancel";
            cancelButton.Location = new System.Drawing.Point(size.Width - 80, 39);
            inputBox.Controls.Add(cancelButton);

            inputBox.AcceptButton = okButton;
            inputBox.CancelButton = cancelButton;

            DialogResult result = inputBox.ShowDialog();
            input = textBox.Text;
            return result;
        }

        private void txtStudent_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
